package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.ResignationApplication;
import org.example.service.ResignationApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/resignation")
public class ResignationApplicationController {

    @Autowired
    private ResignationApplicationService resignationApplicationService;

    /**
     * 获取离职申请列表（分页）
     */
    @GetMapping("/list")
    public Map<String, Object> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String filter) {

        Page<ResignationApplication> page = new Page<>(pageNum, pageSize);
        Page<ResignationApplication> result = resignationApplicationService.getPage(page, keyword, filter);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", result.getRecords());
        response.put("total", result.getTotal());
        response.put("message", "查询成功");

        return response;
    }

    /**
     * 获取离职申请详情
     */
    @GetMapping("/detail/{id}")
    public Map<String, Object> detail(@PathVariable Integer id) {
        ResignationApplication application = resignationApplicationService.getById(id);
        Map<String, Object> response = new HashMap<>();
        if (application != null) {
            response.put("code", 200);
            response.put("data", application);
            response.put("message", "查询成功");
        } else {
            response.put("code", 404);
            response.put("message", "未找到该离职申请");
        }
        return response;
    }

    /**
     * 更新离职申请信息
     */
    @PutMapping("/{id}")
    public Map<String, Object> update(@PathVariable Integer id, @RequestBody ResignationApplication application) {
        // 确保ID一致
        application.setResignationId(id);
        boolean result = resignationApplicationService.updateById(application);
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 500);
            response.put("message", "更新失败");
        }
        return response;
    }

    /**
     * 删除离职申请（批量）
     */
    @DeleteMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> request) {
        try {
            List<Integer> ids = (List<Integer>) request.get("ids");

            if (ids == null || ids.isEmpty()) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 400);
                response.put("message", "请选择要删除的记录");
                return response;
            }

            boolean result = resignationApplicationService.removeByIds(ids);
            Map<String, Object> response = new HashMap<>();
            if (result) {
                response.put("code", 200);
                response.put("message", "删除成功");
            } else {
                response.put("code", 500);
                response.put("message", "删除失败");
            }
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "删除失败: " + e.getMessage());
            return response;
        }
    }

    /**
     * 添加离职申请
     */
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody ResignationApplication application) {
        try {
            boolean result = resignationApplicationService.save(application);

            Map<String, Object> response = new HashMap<>();
            if (result) {
                response.put("code", 200);
                response.put("message", "添加成功");
            } else {
                response.put("code", 500);
                response.put("message", "添加失败");
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "添加失败: " + e.getMessage());
            return response;
        }
    }
}